import 'package:dio/dio.dart';
import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:flutter/services.dart';

///http错误时显示的Widget
///* 根据环境展示不同的Widget，主要区分线上和非线上环境
///* 非线上环境展示错误信息
///* 线上环境展示LogicPage
class HttpErrorWidget extends StatelessWidget {
  final dynamic error;
  final dynamic originalError;
  final bool withAppBarInDebug;

  final LogicBtnClickListener onRetryClickListener;

  HttpErrorWidget(this.error, this.originalError, this.withAppBarInDebug,
      {this.onRetryClickListener});

  @override
  Widget build(BuildContext context) {
    StringBuffer sb = StringBuffer();
    String statusCode;
    String errorCode;
    String errorMsg;
    if (null != error && error is DefaultHttpError) {
      DefaultHttpError defaultHttpError = error;
      sb.writeln("DefaultHttpError开始");
      sb.write(defaultHttpError.toString());
      sb.writeln("DefaultHttpError结束");
      statusCode = null == defaultHttpError.statusCode
          ? "暂无"
          : defaultHttpError.statusCode.toString();
      errorCode = null == defaultHttpError.errorCode
          ? "暂无"
          : defaultHttpError.errorCode.toString();
      errorMsg =
          stringBlank(defaultHttpError.errorMsg) ? "暂无" : defaultHttpError.errorMsg;
    }
    String requestType = "暂无";
    String requestUrl = "暂无";
    String requestParams = "暂无";
    String requestHeader = "暂无";
    String requestErrorMessage = "暂无";
    String requestError = "暂无";
    String requestStackTrace = "暂无";

    if (null != originalError && originalError is DioError) {
      DioError dioError = originalError;
      sb.writeln("Error开始");
      sb.write(dioError.toString());
      sb.writeln("Error结束");

      if (null != dioError.stackTrace) {
        sb.writeln("StackTrace开始");
        sb.write(dioError.stackTrace.toString());
        sb.writeln("StackTracer结束");
      }

      if (null != dioError.request) {
        requestType = dioError.request.method;
        requestUrl = dioError.request.baseUrl + dioError.request.path;
        requestParams = dioError.request.queryParameters.toString();
        requestHeader = dioError.request.headers.toString();
      }

      requestErrorMessage = dioError.message;
      if (null != dioError.error) {
        requestError = dioError.error.toString();
      }

      if (null != dioError.stackTrace) {
        requestStackTrace = dioError.stackTrace.toString();
      }
    }

    Widget content = SingleChildScrollView(
        child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(
                top: 20,
              ),
              child: Row(
                children: <Widget>[
                  MaterialButton(
                    onPressed: () {
                      Clipboard.setData(
                        ClipboardData(
                          text: sb.toString(),
                        ),
                      );
                      showLongToast("已复制到剪切板");
                    },
                    child: Text(
                      "复制到剪切板",
                      style: TextStyle(color: Colors.white),
                    ),
                    color: Colors.blue,
                  ),
                  Padding(
                      padding: EdgeInsets.only(
                        left: 20,
                      ),
                      child: MaterialButton(
                        onPressed: onRetryClickListener,
                        child: Text(
                          "重试",
                          style: TextStyle(color: Colors.white),
                        ),
                        color: Colors.blue,
                      )),
                ],
              ),
            ),
            getHeader("DefaultHttpError"),
            getContentItem("statusCode：", statusCode),
            getContentItem("errorCode：", errorCode),
            getContentItem("errorMsg：", errorMsg),
            getHeader("DioError"),
            getContentItem("requestType：", requestType),
            getContentItem("requestUrl：", requestUrl),
            getContentItem("requestParams：", requestParams),
            getContentItem("requestHeader：", requestHeader),
            getContentItem("requestErrorMessage：", requestErrorMessage),
            getContentItem("requestError：", requestError),
            getContentItem("requestStackTrace：", requestStackTrace),
          ],
        ),
      ],
    ));
    return withAppBarInDebug
        ? Scaffold(
            appBar: DkAppBar(
                DKAppBarParams.withBackNoMenu(context, titleContent: "Http错误")),
            body: content,
          )
        : content;
  }

  final TextStyle headerStyle = TextStyle(fontSize: 18, color: Colors.black);
  final TextStyle titleStyle = TextStyle(fontSize: 16, color: Colors.black);
  final TextStyle contentStyle = TextStyle(fontSize: 16, color: Colors.red);

  Widget getHeader(String header, {double top = 20}) {
    return Padding(
      padding: EdgeInsets.only(top: top),
      child: Text(header, style: headerStyle),
    );
  }

  Widget getContentItem(String title, String content) {
    return Padding(
      padding: EdgeInsets.only(top: 5),
      child: Text.rich(
        TextSpan(
          text: title + "\n",
          style: titleStyle,
          children: <TextSpan>[
            TextSpan(style: contentStyle, text: content),
          ],
        ),
      ),
    );
  }
}
